﻿$(function () {
    var screenwidth, screenheight, mytop, getPosLeft, getPosTop
    screenwidth = $(window).width();
    screenheight = $(window).height();
    //获取滚动条距顶部的偏移
    mytop = $(document).scrollTop();
    //计算弹出层的left
    getPosLeft = screenwidth / 2 - 340;
    //计算弹出层的top
    getPosTop = screenheight / 2 - 150;
    //css定位弹出层
    $("div.popupWindow").css({ "left": getPosLeft, "top": getPosTop });
    //当浏览器窗口大小改变时
    $(window).resize(function () {
        screenwidth = $(window).width();
        screenheight = $(window).height();
        mytop = $(document).scrollTop();
        getPosLeft = screenwidth / 2 - 340;
        getPosTop = screenheight / 2 - 150;
        $("div.popupWindow").css({ "left": getPosLeft, "top": getPosTop + mytop });
    });
    //当拉动滚动条时，弹出层跟着移动
    $(window).scroll(function () {
        screenwidth = $(window).width();
        screenheight = $(window).height();
        mytop = $(document).scrollTop();
        getPosLeft = screenwidth / 2 - 340;
        getPosTop = screenheight / 2 - 150;
        $("div.popupWindow").css({ "left": getPosLeft, "top": getPosTop + mytop });
    });
    //点击链接弹出窗口
    $("input[type='button'].addUpdateRequirement").live("click", function () {
        cleanForm();
        $("div.popupWindow").show("slow");
        if (this.name == "update") {
            $("h2:eq(0)", $("div.popupWindow")).attr("id", "updateNetworkTitle").html("更新申报记录");
            $("input#ID").attr("readonly", true);
            var $parent = $(this).parent().parent();
            $("input#ID").val($.trim($("td:eq(1)", $parent).text()));
            $("input#productName").val($.trim($("td:eq(2)", $parent).text()));
            $("input#number").val($.trim($("td:eq(3)", $parent).text()));
            $("input#description").val($.trim($("td:eq(4)", $parent).text()));
            $("select#networkStatus").find("option[value='" + $.trim($("td:eq(5)", $parent).attr('status')) + "']").attr("selected", true);
            //            $("select#networkStatus").val($.trim($("td:eq(2)", $parent).text()));
            $("input#employee").val($.trim($("td:eq(6)", $parent).text()));
            $("input#requirementDate").val($.trim($("td:eq(7)", $parent).text()));
        } else {
            $("h2:eq(0)", $("div.popupWindow")).attr("id", "newAddNetworkTitle").html("添加申报记录");
            $("input#ID").removeAttr("readonly");
            $("input#ID").val("")
        }
        $("#ID").focus();
        //获取页面文档的高度
        var docheight = $(document).height();
        //追加一个层，使背景变灰
        $("#greybackground").css({ "opacity": "0.5", "height": docheight });
        $("#greybackground").show();
        return false;
    });
    //点击关闭弹出窗口
    $("p#closePopup").click(function () {
        $("div.popupWindow").hide();
        $("#greybackground").hide();
        return false;
    });
    //提交表单
    $("input#btnSubmit").click(function () {
        if ($("input#ID").val() == "") {
            $("span", $("input#ID").parent()).text("账号ID不能为空！");
            return false;
        }
        var requestUrl = "../../network/submit";
        if ($("h2:eq(0)", $("div.popupWindow")).attr("id") == "updateNetworkTitle") {
            requestUrl = "../../network/update";
        }
        $.ajax({
            url: requestUrl,
            type: "POST",
            data: { "MAC": $("input#MAC").val(), "status": $("select#networkStatus option:selected").val(), "assetID": $("input#assetID").val(), "latestUpdateDate": $("#updateDate").val() },
            dataType: "json",
            success: function (data) {
                switch (data.status) {
                    case 1:
                        $("span", $("input#MAC").parent()).text("MAC地址已经存在！");
                        break;
                    case 2:
                        $("div.popupWindow").hide();
                        $("#greybackground").hide();
                        alert("操作成功！");
                        window.location.reload();
                        //                                $("<tr><td>" + $("input#assetID").val() + "</td><td>" + $("input#assetModel").val() + "</td><td>" + $("select#assetType option:selected").text() + "</td><td>" + $("input#assetSn").val() + "</td><td>" + $("select#assetBelongsTo option:selected").text() + "</td><td>" + $("#assetDescription").val() + "</td><td><input class=\"addUpdateAsset\" id=\"btnUpdate\" type=\"button\" name=\"update\" value=\"修改\" /></td></tr>").insertBefore($("#tblMain tr:eq(1)"));
                        break;
                    case 4:
                    default:
                        alert("出错啦，联系管理员！");
                        break;
                }
            }
        });
    });
    //清空表单
    $("input#btnReset").click(function () {
        cleanForm();
    });
    //删除选中网络状态
    $("input#btnDelete").click(function () {
        if ($("input[type='checkbox']:checked", $("#tblMain tr td")).length == 0) {
            alert("请选择需要删除的网络状态");
            return;
        }
        if (confirm("确定删除选中的" + $("input[type='checkbox']:checked", $("#tblMain tr td")).length + "项网络状态?")) {
            var queryData = "";
            var ids = $("input[type='checkbox']:checked", $("#tblMain tr td"));
            $.each(ids, function (i, item) {
                if (i == 0) {
                    queryData = item.id;
                } else {
                    queryData += '||' + item.id;
                }
            });
            $.ajax({
                url: "../../network/delete",
                type: "POST",
                data: { "ids": queryData },
                dataType: "json",
                success: function (data) {
                    switch (data.status) {
                        case 2:
                            alert("操作成功！");
                            window.location.reload();
                            break;
                        case 4:
                        default:
                            alert("出错啦，联系管理员！");
                            break;
                    }
                }
            });
        }
    });
    //checkbox事件
    $("input[type='checkbox']#cbkAll").click(function () {
        if (this.checked) {
            $("input[type='checkbox']", $("#tblMain")).attr("checked", "checked");
        } else {
            $("input[type='checkbox']", $("#tblMain")).removeAttr("checked");
        }
    });
    //checkbox事件
    $("input[type='checkbox']", $("#tblMain tr td")).click(function () {
        if ($("input[type='checkbox']", $("#tblMain tr td")).length == $("input[type='checkbox']:checked", $("#tblMain tr td")).length) {
            $("input[type='checkbox']#cbkAll").attr("checked", "checked");
        } else {
            $("input[type='checkbox']#cbkAll").removeAttr("checked");
        }
    });
    function cleanForm() {
        if ($("input#MAC").attr("readonly") != true) $("input#MAC").val("");
        $("input#status").val("");
        $("input#assetID").val("");
        $("input#lastUpdateDate").val("");
        $("span", $("input#MAC").parent()).text(" *");
    }
});